// ActionScript file
import com.adobe.serialization.json.JSON;

import flash.events.NetStatusEvent;
import flash.net.SharedObject;

import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;

[Bindable]
	public var BASEURL:String	= "fanxiong.eicp.net";
[Bindable]
	public var PORT:String		= "80";
[Bindable]
	public var PATH:String		= "RestaurantPlatform-dev/php/";

public var flushStatus:String=null;

private var mySO:SharedObject;

private function validate():void{				
	/*
	var param:Object = new Object();
	param['username'] = username.text;
	param['passwd']	= passwd.text;
	service.send(param);
	*/
	
}

public function userLogin(username:String, password:String):void
{
	var service:HTTPService = new HTTPService();
	service.url = "http://fanxiong.eicp.net/RestaurantPlatform-dev/php/userValidate.php";
	service.useProxy = false;
	service.method = "GET";
	service.resultFormat = "object";
	var param:Object = new Object();
	param['username'] = username;
	param['passwd']	= password;
	service.addEventListener(ResultEvent.RESULT, userLoginHandle);
	service.addEventListener(FaultEvent.FAULT, httpFault);
	service.send(param);
}

public function userLoginHandle(e:ResultEvent):void{
	var rawData:String = String(e.result);
	var json:Object = (JSON.decode(rawData) as Object);
	if(json.status == 1){
		mySO = SharedObject.getLocal("userInfo");
		mySO.data.username	= json.username;
		mySO.data.post		= json.post;
		mySO.data.truename	= json.truename;
		mySO.data.power		= json.power;
		mySO.data.company_name	= json.company_name;
		try {
			flushStatus = mySO.flush(10000);
		} catch (error:Error) {
			Alert.show("Error...Could not write SharedObject to disk\n", "Error", Alert.OK);
		}
		if (flushStatus != null) {
			switch (flushStatus) {
				case SharedObjectFlushStatus.PENDING:
					Alert.show("pending error", "Error", Alert.OK);
					mySO.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
					break;
				case SharedObjectFlushStatus.FLUSHED:
					//Alert.show("success flush!", "success", Alert.OK);
					break;
			}
		}
		viewstackLogin.selectedChild = businessOperating;
		userInfo.text = "当前操作员:"+mySO.data['truename']+"  所属单位:"+mySO.data['company_name']+"  职位:"+mySO.data['post'];
	}else{
		Alert.show(json.msg, "Error", Alert.OK);
	}
}

public function httpFault(e:FaultEvent):void
{
	
}

public function toggleFullScreen():void {
	if(stage.displayState == "normal"){
		stage.displayState = "fullScreen";
	}else{
		stage.displayState = "normal";
	}
}

public function onFlushStatus(event:NetStatusEvent):void{
	Alert.show("event.info.code:"+event.info.code+"\nflushResult:"+flushStatus, "Info", Alert.OK);
}